from week2.labs.level2_1 import Book

class Author:
    """
    A class representing an author with their collection of books.
    """
    
    def __init__(self, name, nationality="Unknown", birth_year=None):
        """
        Constructor for the Author class.
        
        Args:
            name (str): The name of the author
            nationality (str): The nationality of the author
            birth_year (int): The birth year of the author
        """
        self._name = name
        self._nationality = nationality
        self._birth_year = birth_year
        self._books = []  # Collection of books
    
    # Getters
    def get_name(self):
        return self._name
    
    def get_nationality(self):
        return self._nationality
    
    def get_birth_year(self):
        return self._birth_year
    
    def get_books(self):
        return self._books.copy()  # Return a copy to protect the original list
    
    # Setters
    def set_name(self, name):
        self._name = name
    
    def set_nationality(self, nationality):
        self._nationality = nationality
    
    def set_birth_year(self, birth_year):
        self._birth_year = birth_year
    
    # Methods to manage the book collection
    def add_book(self, book):
        """Add a book to the author's collection."""
        if isinstance(book, Book):
            self._books.append(book)
            print(f"Book '{book.get_title()}' added to {self._name}'s collection")
        else:
            print("Error: Only Book objects can be added")
    
    def remove_book(self, book_title):
        """Remove a book from the author's collection by title."""
        for book in self._books:
            if book.get_title() == book_title:
                self._books.remove(book)
                print(f"Book '{book_title}' removed from collection")
                return True
        print(f"Book '{book_title}' not found in collection")
        return False
    
    def get_number_of_books(self):
        """Return the number of books written by the author."""
        return len(self._books)
    
    def display_books(self):
        """Display all books by the author."""
        if not self._books:
            print(f"{self._name} has no books in the collection")
        else:
            print(f"\nBooks by {self._name}:")
            for i, book in enumerate(self._books, 1):
                print(f"{i}. {book}")
    
    def __str__(self):
        return f"{self._name} ({self._nationality}) - {len(self._books)} book(s)"
    
    def __repr__(self):
        return f"Author('{self._name}', '{self._nationality}', {self._birth_year})"